package io.yuncheng.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.yuncheng.common.exception.RRException;
import io.yuncheng.common.utils.*;
import io.yuncheng.common.validator.ValidatorUtils;
import io.yuncheng.common.utils.Constant;
import io.yuncheng.common.utils.FileUtil;
import io.yuncheng.common.utils.ImportUtil;
import io.yuncheng.modules.sys.entity.SubAppAssessmentEntity;
import io.yuncheng.modules.sys.entity.SubApplicationEntity;
import io.yuncheng.modules.sys.service.SubAppAssessmentService;
import io.yuncheng.modules.sys.service.SubApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;


/**
 * 密评以及风评方案
 *
 * @author Mark
 * @email 15721509625@163.com
 * @date 2020-10-11 16:24:21
 */
@RestController
@Api(tags = "密评管理")
@RequestMapping("sys/subappassessment")
public class SubAppAssessmentController {
    @Autowired
    private SubAppAssessmentService subAppAssessmentService;
    @Autowired
    private SubApplicationService applicationService;

    /**
     * 列表
     */
    @ApiOperation("有效的列表")
    @PostMapping("/list-confirm")
    @RequiresPermissions("sys:subappassessment:list")
    public R listConfirm(@RequestParam Map<String, Object> params) {
        PageUtils page = subAppAssessmentService.queryPageConfirm(params);
        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @ApiOperation("列表")
    @PostMapping("/list")
    @RequiresPermissions("sys:subappassessment:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = subAppAssessmentService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("单条查询")
    @PostMapping("/info/deptIdAndYear")
    @RequiresPermissions("sys:subappassessment:info")
    public R queryByDeptId(Long deptId, Integer year) {
        SubApplicationEntity subApplicationEntity = applicationService.queryByDeptId(deptId);
        if (subApplicationEntity == null) {
            throw new RRException("应用不存在");
        }
        SubAppAssessmentEntity assessmentEntity = subAppAssessmentService.getOne(new QueryWrapper<SubAppAssessmentEntity>().eq("assessment_date", new Date(year - 1900, 0, 1)).eq("app_id", subApplicationEntity.getAppId()).eq("status", Constant.StatusType.NORMAL.getValue()));
        return R.ok().put("subAppAssessment", assessmentEntity);
    }
    /**
     * 信息
     */
    @ApiOperation("密评信息查询")
    @PostMapping("/info/by-dept")
    @RequiresPermissions("sys:subappassessment:info")
    public R queryByDeptId(Long deptId) {
        SubApplicationEntity subApplicationEntity = applicationService.queryByDeptId(deptId);
        if (subApplicationEntity == null) {
            throw new RRException("应用不存在");
        }
        PageUtils pageUtils = subAppAssessmentService.queryByDeptYearStatus(deptId, null, null, null);
        List<SubAppAssessmentEntity> list = (List<SubAppAssessmentEntity>) pageUtils.getList();
        return R.ok().put("page", pageUtils);
    }

    /**
     * 信息
     */
    @ApiOperation("单条查询")
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:subappassessment:info")
    public R info(@PathVariable("id") Long id) {
        SubAppAssessmentEntity subAppAssessment = subAppAssessmentService.getById(id);

        return R.ok().put("subAppAssessment", subAppAssessment);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存")
    @RequiresPermissions("sys:subappassessment:save")
    public R save(@RequestBody SubAppAssessmentEntity subAppAssessment) {
        subAppAssessmentService.save(subAppAssessment);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    @RequiresPermissions("sys:subappassessment:update")
    public R update(@RequestBody SubAppAssessmentEntity subAppAssessment) {
        ValidatorUtils.validateEntity(subAppAssessment);
        subAppAssessmentService.updateById(subAppAssessment);

        return R.ok();
    }
    /**
     * 修改
     */
    @ApiOperation("确认导入")
    @PostMapping("/confirmImport")
    public R confirmImport(Long id) {
        SubAppAssessmentEntity subAppAssessment = new SubAppAssessmentEntity();
        subAppAssessment.setId(id);
        subAppAssessment.setStatus(Constant.StatusType.NORMAL.getValue());
        subAppAssessmentService.updateById(subAppAssessment);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    @RequiresPermissions("sys:subappassessment:delete")
    public R delete(@RequestBody Long[] ids) {
        subAppAssessmentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 上传文件
     */
    @ApiOperation("上传")
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file, int year, Long deptId) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        SubApplicationEntity appId = applicationService.queryByDeptId(deptId);
        SubAppAssessmentEntity entity = ImportUtil.buildByImport(file.getInputStream());
        if (appId == null) {
            throw new RRException("应用不存在");
        }
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String filePath = FileUtil.saveFile(file, uuid);
        entity.setAppId(appId.getAppId());
        entity.setFilePath(filePath);
        entity.setUniqueId(uuid);
        entity.setStatus(Constant.StatusType.OFF.getValue());
        entity.setAssessmentDate(new Date(year - 1900, 0, 1));
        entity.setAppLevel(appId.getAppLevel());
        entity.setAssessmentType(Constant.AssessmentTypeEnum.WIND.getValue());
        Map<String, Object> params = new HashMap<>();
        params.put("deptId",deptId);
        PageUtils pageUtilOld = subAppAssessmentService.queryByDeptYearStatus(deptId,null,null,Constant.StatusType.OFF.getValue());
        //没有 就是密评
        if(!ObjectUtils.isEmpty(pageUtilOld.getList())){
            entity.setAssessmentType(Constant.AssessmentTypeEnum.CONFIDENTIAL.getValue());
        }
        PageUtils pageUtils = subAppAssessmentService.queryByDeptYearStatus(deptId,year,null,Constant.StatusType.NORMAL.getValue());
        List list = pageUtils.getList();
        if(!ObjectUtils.isEmpty(list)){
            throw new RRException("文件已经上传过");
        }
        for (Object object : list) {
            SubAppAssessmentEntity assessmentEntity = (SubAppAssessmentEntity) object;
            assessmentEntity.setStatus(Constant.StatusType.OFF.getValue());
            subAppAssessmentService.updateById(assessmentEntity);
        }
        subAppAssessmentService.save(entity);
        SubAppAssessmentEntity uniqueEntity = subAppAssessmentService.getOne(new QueryWrapper<SubAppAssessmentEntity>().eq("unique_id", uuid));
        return R.ok().put("entity",uniqueEntity);
    }

}
